package md.lanjing.com.module_deal_center.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import md.lanjing.com.module_deal_center.bean.BuyOrderStateResponce;
import com.lxh.baselibray.constant.Constant;
import md.lanjing.com.module_deal_center.contact.BuyOrderStateContact;
import md.lanjing.com.module_deal_center.ui.adapter.BuyOrderStateAdapter;
import retrofit2.Response;

/**
 * 购买订单-已锁定、审核中、已完成
 */
public class BuyOrderStateFragment extends MvpFragment<BuyOrderStateContact.BuyOrderStatePresent> implements BuyOrderStateContact.IBuyOrderStateView {
    public static String TYPE="type";
    @BindView(R2.id.rv)
    RecyclerView rv;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;

    List<BuyOrderStateResponce.DataBean> mList;
    BuyOrderStateAdapter mAdapter;
    int type;//1-已锁定 2-审核中 3-已完成
    int pageNo=1;
    public static BuyOrderStateFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(TYPE, type);
        BuyOrderStateFragment fragment = new BuyOrderStateFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void initData(Bundle savedInstanceState) {
        type=getArguments().getInt(TYPE);

        mList=new ArrayList<>();
        mAdapter=new BuyOrderStateAdapter(R.layout.recy_item_buy_state,mList);
        mAdapter.setType(type);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);

        mPresent.buyOrderState(context,type,pageNo);
        refresh.setOnRefreshListener(refreshLayout -> {
            pageNo=1;
            mPresent.buyOrderState(context,type,pageNo);
        });
        refresh.setOnLoadMoreListener(refreshLayout -> {
            pageNo++;
            mPresent.buyOrderState(context,type,pageNo);
        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_buy_order_state;
    }


    @Override
    public void getBuyOrderStateData(Response<BuyOrderStateResponce> response) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            List<BuyOrderStateResponce.DataBean> data = response.body().getData();
            if (pageNo==1){
                mList.clear();
            }
            mList.addAll(data);
            mAdapter.notifyDataSetChanged();
            if (!ObjectUtils.isEmpty(data)&&data.size()<10){
                refresh.setEnableLoadMore(false);
            }else {
                refresh.setEnableLoadMore(true);
            }
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected BuyOrderStateContact.BuyOrderStatePresent createPresent() {
        return new BuyOrderStateContact.BuyOrderStatePresent();
    }
}
